﻿#ifndef EDITORFACTORY_H
#define EDITORFACTORY_H

#include <QWidget>
#include <QModelIndex>
#include <QLineEdit>
#include <QComboBox>
#include <QSpinBox>
#include <QDoubleSpinBox>
#include <QDebug>

/**
 * @brief 编辑工厂
 */
class EditorFactory
{
public:
    virtual QWidget* createEditor(QWidget* parent, const QModelIndex& index) const = 0;
    virtual void setEditorData(QWidget* editor, const QModelIndex& index) const = 0;
    virtual void setModelData(QWidget* editor, QAbstractItemModel* model,
                              const QModelIndex& index) const = 0;
    virtual ~EditorFactory() = default;

};

/**
 * @brief QLineEdit对应的编辑工厂
 */
class LineEditFactory : public EditorFactory {
public:
    QWidget* createEditor(QWidget* parent, const QModelIndex& index) const override {
        auto w = new QLineEdit(parent);
        w->setText(index.data().toString());
        return w;
    }
    void setEditorData(QWidget* editor, const QModelIndex& index) const override {
        qobject_cast<QLineEdit*>(editor)->setText(index.data().toString());
    }
    void setModelData(QWidget* editor, QAbstractItemModel* model,
                      const QModelIndex& index) const override {
        model->setData(index, qobject_cast<QLineEdit*>(editor)->text());
    }
};

/**
 * @brief QComboBox对应的编辑工厂
 * QComboBox中的列表数据可以用QStandardItem方法中的setData(QJsonArray ,Qt::UserRole + 1)
 * 第一个参数使用QJsonArray存储QComboBox中的下拉数据
 * 例如 QJsonArray jsonList;
 * jsonList.append("123");
 * jsonList.append("456");
 */
class ComboBoxFactory : public EditorFactory {
public:
    QWidget* createEditor(QWidget* parent, const QModelIndex& index) const override {
        auto combo = new QComboBox(parent);

        // 从模型中获取选项数据（假设存储在 Qt::UserRole + 1）
        QStringList items = index.data(Qt::UserRole + 1).toStringList();
        combo->clear();
        combo->addItems(items);

        combo->setCurrentText(index.data().toString());

        return combo;
    }
    void setEditorData(QWidget* editor, const QModelIndex& index) const override {
        auto combo = qobject_cast<QComboBox*>(editor);
        if(!combo)
            return;
        combo->setCurrentText(index.data().toString());
    }
    void setModelData(QWidget* editor, QAbstractItemModel* model,
                      const QModelIndex& index) const override {
        model->setData(index, qobject_cast<QComboBox*>(editor)->currentText());
    }
};

/**
 * @brief QSpinBox对应的编辑工厂
 * QComboBox中的列表数据可以用QStandardItem方法中的setData(QJsonObject ,Qt::UserRole + 1)
 * 第一个参数使用QJsonObject存储QComboBox中的下拉数据
 * 例如：QJsonObject json;
 * json["min"] = 0;
 * json["max"] = 100;
 * json["step"] = 1;
 */
class SpinBoxFactory : public EditorFactory {
public:
    QWidget* createEditor(QWidget* parent, const QModelIndex& index) const override {
        auto spinBox = new QSpinBox(parent);
        spinBox->setValue(index.data().toInt());
        return spinBox;
    }
    void setEditorData(QWidget* editor, const QModelIndex& index) const override {
        qobject_cast<QSpinBox*>(editor)->setValue(index.data().toInt());
    }
    void setModelData(QWidget* editor, QAbstractItemModel* model,
                      const QModelIndex& index) const override {
        model->setData(index, qobject_cast<QSpinBox*>(editor)->value());
    }
};

/**
 * @brief QDoubleSpinBox对应的编辑工厂
 * QComboBox中的列表数据可以用QStandardItem方法中的setData(QJsonObject ,Qt::UserRole + 1)
 * 第一个参数使用QJsonObject存储QComboBox中的下拉数据
 * 例如：QJsonObject json;
 * json["min"] = 0;
 * json["max"] = 100;
 * json["step"] = 0.01;
 */
class DoubleSpinBoxFactory : public EditorFactory {
public:
    QWidget* createEditor(QWidget* parent, const QModelIndex& index) const override {
        auto spinBox = new QDoubleSpinBox(parent);
        spinBox->setSingleStep(0.01);
        spinBox->setValue(index.data().toDouble());
        return spinBox;
    }
    void setEditorData(QWidget* editor, const QModelIndex& index) const override {
        qobject_cast<QDoubleSpinBox*>(editor)->setValue(index.data().toDouble());
    }
    void setModelData(QWidget* editor, QAbstractItemModel* model,
                      const QModelIndex& index) const override {
        model->setData(index, qobject_cast<QDoubleSpinBox*>(editor)->value());
    }
};

#endif // EDITORFACTORY_H
